# -*- coding: utf-8 -*-

# Created on 08/08/2010

# Copyright (C) 2010  Sergio Rodrigo

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


#from debug.Logger import Log

from core.Control import *
from core.Action import Action
from core.Key import *

ACTION_QUIT = "Quit"
ACTION_MOVE_UP = "Move Up"
ACTION_MOVE_DOWN = "Move Down"

class PongKeyboardControl(Control):

    def __init__(self, name, keyboard, controllableActor, keysActions):
        Control.__init__(self, name, keyboard, controllableActor)
        #Sample: {KEY_ESCAPE : ACTION_QUIT,
        #        KEY_UP : ACTION_MOVE_UP,
        #        KEY_DOWN : ACTION_MOVE_DOWN}
        self.keysActions = keysActions
    
    def _makeActions(self):
        """ Reads data in the peripheral and makes the actions associated.
        """
        data = self.peripheral.readData()
        while data:
            #Log.debug(data)
            actionName = None
            actionType = None
            actionValue = data[0]
            actionMagnitude = data[1]
            #if actionValue == KEY_ESCAPE:
            if self.keysActions.has_key(actionValue):
                if self.keysActions[actionValue] == ACTION_QUIT:
                    actionName = ACTION_QUIT
                    actionType = INTERRUPTION_ACTION
                else:
                    actionName = self.keysActions[actionValue]
                    actionType = ACTOR_ACTION
            #elif actionValue == KEY_UP:
            #elif self.keysActions[actionValue] == ACTION_MOVE_UP:
            #    actionName = "Move Up"
            #    actionType = ACTOR_ACTION
            #elif actionValue == KEY_DOWN:
            #elif self.keysActions[actionValue] == ACTION_MOVE_DOWN:
            #    actionName = "Move Down"
            #    actionType = ACTOR_ACTION
            # Aqui, se ajusta el valor de la magnitud logica de la accion mediante la magnitud fisica
            action = Action(actionType, actionName, actionValue, actionMagnitude)
            if actionType == ACTOR_ACTION:
                self.actions.append(action)
            elif actionType == INTERRUPTION_ACTION:
                self.interruptions.append(action)

            data = self.peripheral.readData()

# eof
